package com.yx.frame.jdbc.pool;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

public final class PoolManager {
	private static PoolManager instance = null;
	Hashtable<String, Pool> pools = new Hashtable<String, Pool>();
	String defaultDatabaseName = null;
	
	private PoolManager() throws Exception {		
	}
	public String getDefaultDatabaseName() {
		return defaultDatabaseName;
	}
	public Pool getPool(String name)throws Exception{
		Pool pool = null;
		try{
			pool=(Pool) pools.get(name);		
		}catch(Exception e){
			pool = null;
		}
		if(pool==null)throw new Exception("数据源["+name+"]不存在");
		return pool;
	}
	public int getMaxQueryRowCount(String JndiName)throws Exception {
		Pool poolable = getPool(JndiName);
		return poolable.getMaxQueryRowCount();
	}

	public Enumeration<String> listJDBCPoolName() {
		return pools.keys();
	}

	public void init(List<JdbcConfig> poolList) throws Exception {
		defaultDatabaseName = null;
		if (poolList == null||poolList.size()<1) {
			return;
		}
		for (JdbcConfig config:poolList) {
			String pName = config.getId();
			if(config.isDefaultJdbc()){
				if (defaultDatabaseName == null) {
					defaultDatabaseName = pName;					
				} else {
					throw new Exception("不能存在两个默认数据库连接池！");
				}
			}			
			String FramePool ="N";
			String JDBCDriver = config.getDriver();
			String JDBCUrl = config.getUrl();
			String JDBCUser =config.getUserName();
			String JDBCPassword = config.getPassword();
			int MinConnectionCount = config.getMinConnectionSize();
			int MaxConnectionCount = config.getMaxConnectionSize();
			int GetConnectionTimeout = config.getGetConnectionTimeout();
			int MaxQueryRowCount = config.getMaxQueryRowCount();
			int MaxIdleTime = config.getMaxIdleTime();
			int AcquireIncrement = config.getAcquireIncrement();
			Pool pool;
			if("Y".equals(FramePool)){
				pool = new JPool(pName, JDBCDriver, JDBCUrl,
					JDBCUser, JDBCPassword, MinConnectionCount,
					MaxConnectionCount, GetConnectionTimeout, MaxQueryRowCount,
					MaxIdleTime, AcquireIncrement);
//			}else{
//				pool = new C3p0Pool(pName, JDBCDriver, JDBCUrl,
//						JDBCUser, JDBCPassword, MinConnectionCount,
//						MaxConnectionCount, GetConnectionTimeout, MaxQueryRowCount,
//						MaxIdleTime, AcquireIncrement);
			}else{
				pool = new WebLogicPool(pName, MaxQueryRowCount);
			}
			pools.put(pName, pool);
		}
		if (defaultDatabaseName == null) {
			throw new Exception("没有设定默认数据库连接池！");
		}
	}
	public static PoolManager getInstance() throws Exception {
		if (instance == null) {
			instance = new PoolManager();
		}
		return instance;
	}
	public PConnection getPoolConnection(String JndiName) throws Exception {
		Pool poolable = getPool(JndiName);
		PConnection pConnection = poolable.getConnection();
		return pConnection;
	}
	public void freeFramePoolConnection(String JndiName, PConnection jo)
			throws Exception {
		JPool pool = (JPool) getPool(JndiName);
		pool.freeConnection(jo);
	}

}
